<template>
    <div id="issue-list">
        <el-dialog
            :title="titleCon"
            v-model="listDialog" 
            draggable 
            :close-on-click-modal="false" 
            width="56%" 
            :before-close="handleClose"
            :destroy-on-close="true"
            @close="clean"
        >
            <el-form :model="ruleForm" ref="ruleFormRef" size="default" label-width="120px" :rules="rules">
                <el-row>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item label="隐患分类" prop="v_Type">
                            <el-select 
                                filterable
                                class="m-2" 
                                placeholder="请选择隐患类型" 
                                v-model="ruleForm.v_HiddenDangerClass" 
                            >
                                <el-option
                                    v-for="item in typeOption"
                                    :key="item.id"
                                    :label="item.value"
                                    :value="item.value"
                                />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item label="隐患问题描述" prop="v_Problem">
                            <el-input type="textarea" autosize v-model="ruleForm.v_Problem" placeholder="请输入隐患问题描述" clearable />
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <template #footer>
                <el-button @click="cancelUseClick">取消</el-button>
                <el-button type="primary" :disabled="repeatDis" :loading="repeatDis" @click="submitForm(ruleFormRef)">提 交</el-button>
            </template>
        </el-dialog>
    </div>
</template>
<script lang="ts">
import { defineComponent, ref, reactive , computed , toRef , toRefs , toRaw , onMounted , watch } from 'vue';
import { FormInstance, FormRules, ElButton, ElCol, ElDialog, ElForm, ElFormItem, ElOption, ElRow, ElSelect } from 'element-plus';
import { SMSDCSafetyProblemAdd, SMSDCSafetyProblemUpdate } from '/@/api/centerControl/sms';
import { sysDictDataList } from '/@/api/centerControl/pubilc';
/*** 定义接口基础数据类型 */
interface formType {
    repeatDis: Boolean,
    ruleForm: ruleFormType,
}
/*** form 表单数据类型 */
type ruleFormType = {
    v_Problem: String, // 问题描述
    v_HiddenDangerClass: String, // 隐患分类
    l_SMSDCListId: Number, // 风险管控记录id
}
export default defineComponent( {
    name: 'issue-list',
    components: {},
    props: {
        listDialog:{
            type: Boolean,
            default: false,
        },
        listInfo: {
            type: String,
            default: ''
        }
    },
    setup(props,ctx){
        const titleCon = ref();
        const ruleFormRef = ref();
        const typeOption = ref();

        /*** state 引用接口定义基础数据初始值 */
        const state = reactive < formType > ({
            repeatDis: false,
            ruleForm: {} as ruleFormType
        })

        /*** 基础验证 rules */
        const rules = reactive < FormRules > ({
            /*** 放置验证 */
            // 问题描述
            v_Problem: [
				{ required: true, message: '请选择问题描述', trigger: 'blur' },
			],
            // 隐患分类
            v_HiddenDangerClass: [
                { required: true, message: '请选择隐患分类', trigger: 'change' },
            ],
        })

        /*** handleClose 关闭按钮点击事件 */
        const handleClose = () => {
            cancelUseClick();
        }

        /*** cancelUseClick 取消事件 */
        const cancelUseClick = () => {
            ctx.emit('cancelListClick')
        }

        /*** submitForm 提交事件 */
        const submitForm = (formEl: FormInstance | undefined) => {
            if(!formEl) return
            formEl.validate( async (valid) => { 
                if(valid){
                    let param: any = toRaw(state.ruleForm);
                    console.log(param,'param')
                    state.repeatDis = true;
                    try{
                        let res: any;
                        if(JSON.parse(props.listInfo).status === 'add'){
                            res =  await SMSDCSafetyProblemAdd(param);
                        }else{
                            res =  await SMSDCSafetyProblemUpdate(param); 
                        }
                        if(res.success){
                            state.repeatDis = false;
                            ctx.emit('updateData');
                            ctx.emit('controlListClick');
                        }else{
                            state.repeatDis = false;
                            ctx.emit('controlListClick');
                        }
                    }catch(error){ state.repeatDis = false; }
                }else{
                    console.log('error submit!')
					return false
                }
            })
        }

        /*** clean 清空数据 */
        const clean = () => {
            ruleFormRef.value.resetFields()
        }

        /*** getData 获取数据 */
        const getData = async() => {
            let ras: any = await sysDictDataList({ TypeCode: 'HiddenDangerType' });
            typeOption.value = ras.data;
        }

        /*** 监听listDialog的变化 */
        watch(() => props.listDialog,(newVal) => {
            if(newVal){
                getData();
                state.repeatDis = false;
                if(JSON.parse(props.listInfo).status === 'add'){
                    titleCon.value = '风险管控清单';
                    state.ruleForm = {
                        v_Problem: '', // 问题描述
                        v_HiddenDangerClass: '', // 隐患分类
                        l_SMSDCListId: JSON.parse(props.listInfo).id, // 风险管控记录id
                    }
                }else{
                    titleCon.value = '风险管控清单';
                    state.ruleForm = JSON.parse(props.listInfo).info;
                }
            }
        })

        /*** listDialog 响应弹窗的关闭  */
        const listDialog = computed(() => {
            return props.listDialog
        })

        return {
            handleClose,
            submitForm,
            cancelUseClick,
            clean,
            ruleFormRef,
            titleCon,
            ...toRefs(state),
            listDialog,
            rules,
            typeOption,
        }
    },
} )
</script>
<style scoped lang="scss">
:deep(.el-dialog__body){
    height: auto!important;
    max-height: none!important;
    overflow-y: hidden;
}
:deep(.el-cascader){
    width: 100%;
}
:deep(.el-select){
    width: 100%!important;
}
:deep(.el-input){
    width: 100%;
}
</style>